package hyit.app.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import hyit.app.dao.IClientUserDAO;
import hyit.app.model.ClientUser;

public class ClientUserDAOImpl implements IClientUserDAO {
	private Connection conn = null;
	private PreparedStatement pstmt = null;

	public ClientUserDAOImpl(Connection conn) {
		this.conn = conn;
	}

	@Override
	public boolean doCreate(ClientUser clientUser) throws Exception {
		boolean flag = false;
		String sql = "INSERT INTO client_user VALUES(NULL,?,?,?,?)";
		this.pstmt = this.conn.prepareStatement(sql);
		this.pstmt.setLong(1, clientUser.getMemberId());
		this.pstmt.setString(2, clientUser.getPassword());
		this.pstmt.setString(3, clientUser.getName());
		this.pstmt.setString(4, clientUser.getCardMac());
		if (this.pstmt.executeUpdate() > 0) {
			flag = true;
		}
		this.pstmt.close();
		return flag;
	}

	@Override
	public boolean update(ClientUser clientUser) throws Exception {
		boolean flag = false;
		String sql = "UPDATE client_user SET `password` = ? AND card_mac = ? WHERE memberid = ?";
		this.pstmt = this.conn.prepareStatement(sql);
		this.pstmt.setString(1, clientUser.getPassword());
		this.pstmt.setString(2, clientUser.getCardMac());
		this.pstmt.setLong(3, clientUser.getMemberId());
		if (this.pstmt.executeUpdate() > 0) {
			flag = true;
		}
		this.pstmt.close();
		return flag;
	}

	@Override
	public boolean delete(ClientUser clientUser) throws Exception {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public ClientUser findById(Long number) throws Exception {
		ClientUser clientUser = null;
		String sql = "SELECT id,memberid,`password`,`name`,card_mac FROM client_user WHERE memberid = ?";
		this.pstmt = this.conn.prepareStatement(sql);
		this.pstmt.setLong(1, number);
		ResultSet rs = this.pstmt.executeQuery();
		while (rs.next()) {
			clientUser = new ClientUser();
			clientUser.setId(rs.getInt("id"));
			clientUser.setMemberId(rs.getLong("memberid"));
			clientUser.setPassword(rs.getString("password"));
			clientUser.setName(rs.getString("name"));
			clientUser.setCardMac(rs.getString("card_mac"));
		}
		this.pstmt.close();
		return clientUser;
	}

	@Override
	public ClientUser validate(Long memberId, String password) throws Exception {
		ClientUser clientUser = null;
		String sql = "SELECT id,memberid,`password`,`name`,card_mac FROM client_user WHERE memberid = ? AND `password` = ?";
		this.pstmt = this.conn.prepareStatement(sql);
		this.pstmt.setLong(1, memberId);
		this.pstmt.setString(2, password);
		ResultSet rs = this.pstmt.executeQuery();
		while (rs.next()) {
			clientUser = new ClientUser();
			clientUser.setId(rs.getInt("id"));
			clientUser.setMemberId(rs.getLong("memberid"));
			clientUser.setPassword(rs.getString("password"));
			clientUser.setName(rs.getString("name"));
			clientUser.setCardMac(rs.getString("card_mac"));
		}
		this.pstmt.close();
		return clientUser;
	}

	@Override
	public ClientUser validate(String cardMac) throws Exception {
		ClientUser clientUser = null;
		String sql = "SELECT id,memberid,`password`,`name`,card_mac FROM client_user WHERE card_mac = ?";
		this.pstmt = this.conn.prepareStatement(sql);
		this.pstmt.setString(1, cardMac);
		ResultSet rs = this.pstmt.executeQuery();
		while (rs.next()) {
			clientUser = new ClientUser();
			clientUser.setId(rs.getInt("id"));
			clientUser.setMemberId(rs.getLong("memberid"));
			clientUser.setPassword(rs.getString("password"));
			clientUser.setName(rs.getString("name"));
			clientUser.setCardMac(rs.getString("card_mac"));
		}
		this.pstmt.close();
		return clientUser;
	}

}
